using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using StateManagement.Models;

namespace StateManagement.Controllers {
  public class BooksController : Controller {
    private readonly BookDbContext _context;

    public BooksController(BookDbContext context) {
      _context = context;
    }

    // GET: Books
    public async Task<IActionResult> Index() {
      return View(await _context.Books.ToListAsync());
    }

    // GET: Books/Details/5
    public async Task<IActionResult> Details(int? id) {
      if (id == null) {
        return NotFound();
      }

      var book = await _context.Books
          .FirstOrDefaultAsync(m => m.Id == id);
      if (book == null) {
        return NotFound();
      }

      return View(book);
    }

    // GET: Books/Create
    public IActionResult Create() {
      return View();
    }

    // POST: Books/Create
    // To protect from overposting attacks, enable the specific properties you want to bind to.
    // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("Id,ISBN,Title,Author,Year,Price")] Book book) {
      if (ModelState.IsValid) {
        _context.Add(book);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
      }
      return View(book);
    }

    // GET: Books/Edit/5
    public async Task<IActionResult> Edit(int? id) {
      if (id == null) {
        return NotFound();
      }

      var book = await _context.Books.FindAsync(id);
      if (book == null) {
        return NotFound();
      }
      return View(book);
    }

    // POST: Books/Edit/5
    // To protect from overposting attacks, enable the specific properties you want to bind to.
    // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(int id, [Bind("Id,ISBN,Title,Author,Year,Price")] Book book) {
      if (id != book.Id) {
        return NotFound();
      }

      if (ModelState.IsValid) {
        try {
          _context.Update(book);
          await _context.SaveChangesAsync();
        } catch (DbUpdateConcurrencyException) {
          if (!BookExists(book.Id)) {
            return NotFound();
          } else {
            throw;
          }
        }
        return RedirectToAction(nameof(Index));
      }
      return View(book);
    }

    // GET: Books/Delete/5
    public async Task<IActionResult> Delete(int? id) {
      if (id == null) {
        return NotFound();
      }

      var book = await _context.Books
          .FirstOrDefaultAsync(m => m.Id == id);
      if (book == null) {
        return NotFound();
      }

      return View(book);
    }

    // POST: Books/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> DeleteConfirmed(int id) {
      var book = await _context.Books.FindAsync(id);
      if (book != null) {
        _context.Books.Remove(book);
      }

      await _context.SaveChangesAsync();
      return RedirectToAction(nameof(Index));
    }

    private bool BookExists(int id) {
      return _context.Books.Any(e => e.Id == id);
    }
  }
}
